/**
 * 食物相关的对象
 */
(function (window, undefined) {

    /**
     * @param {画布} map 
     * @param {属性} options   width  height   color   x   y 
     */
    function Food(map, options) {
        this.map = map;
        options = (options instanceof Object) ? options : {};
        this.width = options.width || 20;
        this.height = options.height || 20;
        this.color = options.color || "blue";
        this.x = options.x || 0;
        this.y = options.y || 0;
        this.elements = [];//记录食物
    }

    /**
     * 渲染食物
     */
    Food.prototype.render = function () {
        //计算食物可以占多少份 offset包括了边框
        const partWidth = this.map.clientWidth / this.width - 1;
        const partHeight = this.map.clientHeight / this.height - 1;
        console.log(partWidth, partHeight);

        const ele = document.createElement("div");
        this.map.appendChild(ele);
        ele.style.width = this.width + "px";
        ele.style.height = this.height + "px";
        ele.style.backgroundColor = this.color;
        ele.style.left = Tools.getRandom(0, partWidth) * this.width + "px";
        ele.style.top = Tools.getRandom(0, partHeight) * this.height + "px";
        ele.style.position = "absolute";
        this.elements.push(ele);
    }

    Food.prototype.remove = function (index) {
        const el = this.elements[index];
        this.map.removeChild(el);
    }

    //对外暴露Food对象
    window.Food = Food;
})(window, undefined);

// const map = document.getElementById("map");
// const food = new Food(map);
// food.render();
// food.render();
// food.render();